package com.kalix.mooc.reply.biz;

import com.kalix.framework.core.api.persistence.JsonData;
import com.kalix.framework.core.api.persistence.JsonStatus;
import com.kalix.framework.core.impl.biz.ShiroGenericBizServiceImpl;
import com.kalix.mooc.reply.api.biz.INoteBeanService;
import com.kalix.mooc.reply.api.dao.INoteBeanDao;
import com.kalix.mooc.reply.api.dto.DiscussDTO;
import com.kalix.mooc.reply.api.dto.NoteDTO;
import com.kalix.mooc.reply.entities.NoteBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 功能描述:
 *
 * @Description: 笔记实现类
 * @Author: yyp
 * @CreateDate: 2019/5/25$ 14:59$
 * @updateDate: 2019/7/04$ 18:38$
 * @Version:
 */
public class NoteBeanServiceImpl extends ShiroGenericBizServiceImpl<INoteBeanDao, NoteBean> implements INoteBeanService {
    @Override
    /**
     * 增加笔记 2019/05/25
     * */
    public JsonStatus addNote(NoteBean note) {
        NoteBean noteBean = new NoteBean();
        noteBean.setUserId(note.getUserId());
        noteBean.setUserName(note.getUserName());
        noteBean.setNoteTitle(note.getNoteTitle());
        noteBean.setNoteEnclosure(note.getNoteEnclosure());
        noteBean.setNoteContent(note.getNoteContent());
        noteBean.setNoteUpcounts(0l);
        noteBean.setCid(note.getCid());
        noteBean.setCname(note.getCname());
        noteBean.setCreationDate(new Date());
        NoteBean noteBean1 = dao.save(noteBean);
        JsonStatus jsonS = new JsonStatus();
        if (noteBean1 != null) {
            jsonS.setSuccess(true);
            jsonS.setMsg("保存成功！");
        } else {
            jsonS.setFailure(true);
            jsonS.setMsg("保存失败！");
        }
        return jsonS;
    }

    public JsonData getAllNote(String cid) {
        String sql = "select n.id,n.noteTitle,n.noteContent,n.noteEnclosure,n.noteUpcounts,n.userId,n.userName,n.cid,n.cname from mooc_note n, mooc_course c where c.id = n.cid";
        if (!cid.isEmpty() && cid!=null) {
            sql += " and n.cid=" + cid;
        }
        List<NoteDTO> list = dao.findByNativeSql(sql, NoteDTO.class);
        JsonData jsonData = new JsonData();
        jsonData.setData(list);
        jsonData.setTotalCount((long) list.size());
        return jsonData;
    }


//    public String getTime(){
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//        String time = df.format(new Date());// new Date()为获取当前系统时间
//        return time;
//    }
}
